﻿using Microsoft.AspNetCore.SignalR;
using MultiAgentsClient.ChatDataStructures;
using MultiAgentsShared;
using static MultiAgentsServer.Scripts.Services.AgentService;
namespace MultiAgentsServer.Scripts.Hubs;

public class SignalRAgentHub(IEventBus eventBus) : SignalRBaseHub(eventBus)
{
    #region ApiRequest
    public void CreateNewAgentRequest(Dictionary<string, object> requestParams)
    {
        var sessionId = requestParams.GetData<string>("sessionId");
        var userId = requestParams.GetData<int>("userId");

        _eventBus.Publish(new CreateNewAgentCommand(Context.ConnectionId, sessionId, userId));
    }

    public void GenerateMessageRequest(Dictionary<string, object> requestParams)
    {
        var sessionId = requestParams.GetData<string>("sessionId");
        var userId = requestParams.GetData<int>("userId");
        var messagesJson = requestParams.GetData<List<string>>("messages");
        var temperature = requestParams.GetData<float>("temperature");
        var model = requestParams.GetData<string>("model");

        List<ChatMessageData> messages = [];
        foreach (string message in messagesJson)
        {
            messages.Add(SerializableObjectBase.FromJson<ChatMessageData>(message));
        }

        _eventBus.Publish(new GenerateMessageCommand(Context.ConnectionId, sessionId, userId, messages, temperature, model));
    }

    public void LoadAgentByShareCodeRequest(Dictionary<string, object> requestParams)
    {
        var sessionId = requestParams.GetData<string>("sessionId");
        var userId = requestParams.GetData<int>("userId");
        var agentJson = requestParams.GetData<string>("agentJson");

        _eventBus.Publish(new LoadAgentByJsonRequestCommand(Context.ConnectionId, sessionId, userId, agentJson));
    }

    public void RemoveAgentRequest(Dictionary<string, object> requestParams)
    {
        var sessionId = requestParams.GetData<string>("sessionId");
        var userId = requestParams.GetData<int>("userId");
        var agentId = requestParams.GetData<int>("agentId");

        _eventBus.Publish(new RemoveAgentCommand(Context.ConnectionId, sessionId, userId, agentId));
    }

    public void UpdateAgentRequest(Dictionary<string, object> requestParams)
    {
        var sessionId = requestParams.GetData<string>("sessionId");
        var userId = requestParams.GetData<int>("userId");
        var agentTemplateJson = requestParams.GetData<string>("agentTemplate");
        AgentTemplate agentTemplate = SerializableObjectBase.FromJson<AgentTemplate>(agentTemplateJson); 

        _eventBus.Publish(new UpdateAgentCommand(Context.ConnectionId, sessionId, userId, agentTemplate));
    }
    #endregion Requests

    #region ApiResponse
    //API Response
    public static async Task CreateNewAgentResponse(IHubContext<SignalRAgentHub> hubContext, string connectionId, AgentTemplate agentTemplate)
    {
        string agentTemplateJson = agentTemplate.ConvertToJson();
        await hubContext.Clients.Client(connectionId).SendAsync("CreateNewAgentResponse", agentTemplateJson);
    }

    public static async Task GenerateMessageResponse(IHubContext<SignalRAgentHub> hubContext, string connectionId, string responseMessage)
    {
        await hubContext.Clients.Client(connectionId).SendAsync("GenerateMessageResponse", responseMessage);
    }

    public static async Task RemoveAgentResponse(IHubContext<SignalRAgentHub> hubContext, string connectionId, int AgentId)
    {
        await hubContext.Clients.Client(connectionId).SendAsync("RemoveAgentResponse", AgentId);
    }

    public static async Task UpdateAgentResponse(IHubContext<SignalRAgentHub> hubContext, string connectionId, AgentTemplate agentTemplate)
    {
        string agentTemplateJson = agentTemplate.ConvertToJson();
        await hubContext.Clients.Client(connectionId).SendAsync("UpdateAgentResponse", agentTemplateJson);
    }
    #endregion
}
